import { login, logout, getInfo,getDefaultUser } from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  getCountdown,
  setCountdown,
  removeCountdown,
  getLogin,
  setLogin,
  removeLogin,
  getUserInfo,
  setUserInfo,
  getIsAdmin,
  setIsAdmin,
  removeIsAdmin,
  removeUserInfo
} from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: getToken(),
    avatar: '',
    // 用户信息
    userInfo: getUserInfo(),
    // 倒计时
    countdownTime: getCountdown(),
    // 是否已经登录
    isLogin: getLogin(),
    // 是否是管理员
    isAdmin:getIsAdmin()
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
    state.isLogin = true;
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_IS_LOGIN: (state, isLogin) => {
    state.isLogin = isLogin
  },
  SET_COUNTDOWN: (state, countdownTime) => {
    state.countdownTime = countdownTime
  },
  SET_IS_ADMIN: (state, isAdmin) => {
    state.isAdmin = isAdmin
  },
  SET_NAME: (state, name) => {
    state.name = name
  }
}

const actions = {
  // 登录
  login({ commit }, userInfo) {
    const { username, password,type } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password ,type:type}).then(response => {
        commit('SET_TOKEN', response.result.token)
        commit('SET_COUNTDOWN', response.result.timestamp)
        commit('SET_IS_LOGIN', true)
        setToken(response.result.token)
        setCountdown(response.result.timestamp)
        setLogin(true)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { result } = response
        if (!result) {
          return reject('验证失败，请重新登录！')
        }
        commit('SET_IS_ADMIN', result.type === 1)
        setIsAdmin(result.type === 1)
        commit('SET_USERINFO', result)
        commit('SET_NAME', result.nickName)
        setUserInfo(result)
        resolve(result)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取默认用户信息
  getDefaultUser({ commit, state }) {
    return new Promise((resolve, reject) => {
      getDefaultUser().then(response => {
        const { result } = response
        if (!result) {
          return reject('验证失败，请重新登录！')
        }
        commit('SET_USERINFO', result)
        commit('SET_NAME', result.nickName)
        setUserInfo(result)
        resolve(result)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出登录
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        // must remove  token  first
        removeToken()
        removeLogin()
        removeCountdown()
        removeIsAdmin()
        resetRouter()
        removeUserInfo()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 清楚token
  resetToken({ commit }) {
    return new Promise(resolve => {
      // 必须先删除令牌
      removeToken()
      removeLogin()
      removeCountdown()
      removeIsAdmin()
      removeUserInfo()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

